/****************************************************************************
**
** Authors: Antonio Marcio A Menezes, Emanuel Grohs.
**
** This file is part of the Qt Board Game Framework.
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "zone.h"

using namespace qtbf;

/* A class for logic division of board mechanics and game flow. */
// TODO fazer as Zones terem props como minDistX e minDistY passadas no contrutor
Zone::Zone(QObject *parent) : QObject(parent)
  //(String name, IPlayer[] owners, /* Zone conteudo, */VISIBILITY visibility, boolean movable) {
{
    if (owners != null) {
            this.owners = Arrays.asList(owners);
    }
    // TODO remover isso???
    setName(name);
    if (!(this instanceof CartaBaralho)) {
            setBorder(BorderFactory.createTitledBorder(name));
    }
    this.visibility = visibility;
    this.movable = movable;
    setMinimumSize(new Dimension(GUIPreferencias.deckX, GUIPreferencias.deckY));
    reorganiza();
    // setLayout(new CircularLayoutStrategy());
}

/* An Enum for zone visibility renderization. */
public static enum VISIBILITY {
    CLOSED, PRIVATE, /* ALL */PUBLIC;
}

/* Zone visibility over GUI. */
protected VISIBILITY visibility;

/* List of player owners of this zone. Only owners can modify zone values. */
protected List<IPlayer> owners;

/* If this zone can be dragged over the board. */
protected boolean movable;

/* If this zone was selected by a mouse event. See multiple zone selection. */
transient protected boolean selected;

public List<IPlayer> getOwners() {
        return owners;
}

/* Add other zone inside this one. */
public Zone add(Zone child) {
        return add(child, -1);
}

/* Add other zone inside this one at given position. */
public Zone add(Zone child, int index) {
        if (!(child instanceof Zone)) {
                // Do nothing
                System.err.println("Zones can have Zone childs only!");
                return null;
        }
        // Container antesMove = child.getParent();
        super.add(child, index);
        Zone novaZone = (Zone) child;
        // Quando uma Zone muda de local ela muda de dono.
        novaZone.setowners(owners);
        novaZone.visibility = visibility;
        reorganiza();
        // firePropertyChange("moveu", antesMove, this);
        return child;
}

/* Remove a zone child. */
public void remove(Zone child) {
        super.remove(child);
        reorganiza();
        // firePropertyChange("childonent", 4, 5);
}

/*
 * public Set<String> getowners() { return owners; }
 *
 * public Set<String> getvisibility() { return visibility; }
 */

public void setowners(List<IPlayer> owners) {
        this.owners = owners;
}

public void setowners(IPlayer[] owners) {
        this.owners = Arrays.asList(owners);
}

/*
 * public void setvisibility(Set<String> visibility) { this.visibility =
 * visibility; }
 */

public boolean isSelected() {
        return selected;
}

public void setSelected(boolean selected) {
        this.selected = selected;
}

// public TipoZone getTipoZone() {
// return tipoZone;
// }
//
// public void setTipoZone(TipoZone tipoZone) {
// this.tipoZone = tipoZone;
// }
//

/* Returns true if playerName owns or can see zone content. */
public boolean isVisibleOwns(String playerName) {
        if (visibility == VISIBILITY.PUBLICA
                        || (visibility != VISIBILITY.CLOSED && owners != null && owners.contains(playerName))) {
                return true;
        }
        return false;
}

/* Returns true if playerName owns this zone. */
public boolean possuida(String playerName) {
        return owners != null && owners.contains(playerName);
}

/* Returns topmost parent zone in this zone hierarchy. */
// TODO aki? Melhorar isso
public Zone getPrimaryZone(Zone z) {
        if (z.getParent() instanceof Zone) {
                return getZonePrimaria((Zone) this.getParent());
        }
        return z;
}

/* Returns child count. */
public int lenght() {
        return getchildonentCount();
}

//
// public void setvisibility(visibility visibility) {
// this.visibility = visibility;
// }

public String toString() {
        return getName();
}

public void setaPicture(String namePlayer) {
        // TODO Auto-generated method stub

}
